<template>
	<section>
		<!--高级查询工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="query">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <el-table :data="dictionaryTypes" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="id" label="编号" width="200" sortable>
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" sortable>
      </el-table-column>
        <el-table-column prop="sn" label="字典编号"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--批量删除工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑或新增界面-->
		<el-dialog :title="myTitle" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
        myTitle:"",
				filters: {
					keyword: '' //关键字查询
				},
        dictionaryTypes: [],//部门管理员数据
        deptTree:[],//上级部门无级限树
				total: 0,
        pageSize:5, //每页展示条数
        currentPage: 1, //当前页码
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {//表单中那些属性需要校验
          name: [
						{ required: true, message: '请输入字典名', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入字典编号', trigger: 'blur' }
          ]
				},
        //编辑界面数据
        editForm: {
          id: null,
          sn: "",
          name:""

        }

			}
		},
		methods: {
      query(){
        //高级查询完定位到第一页
        this.currentPage = 1;
        this.getDictionaryTypes();
      },
			handleCurrentChange(val) {
			  //当前页码
				this.currentPage = val;
				this.getDictionaryTypes();
			},
			//获取部门列表
      getDictionaryTypes() {
				let para = {
          currentPage: this.currentPage,//页码
          pageSize: this.pageSize,//每页展示条数
					keyword: this.filters.keyword//关键字
				};
				this.listLoading = true;//开启列表忙等
        this.$http.post("/type",para)//高级查询 + 分页
            .then(result=>{
              this.listLoading = false;//关闭列表忙等
              result  = result.data;//获取回调数据的数据
              this.dictionaryTypes = result.rows;//回调数据的rows给到departments
              this.total = result.total;//回调数据的总条数
            })
        .catch(result=>{//异常弹出
          console.log(result);
          this.$message({
            message: '网络繁忙,请稍后重试',
            type: 'error'
          });
        })
			},
      //获取员工数据
      /*getEmployees(){
        this.$http.get("/employee")
          .then(result=>{
            this.employees = result.data;
            console.log(this.employees);
          })
      },*/
      //上级部门无级限树
      /*getDeptTree(){
        this.$http.get("/type/tree")
         .then(result=>{
           this.deptTree = result.data;
           console.log(this.this.deptTree);
        })
      },*/

			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				})
        .then(() => {
					this.listLoading = true;//开启忙等加载
          this.$http.delete("/type/"+row.id)
          .then(result=>{
            this.listLoading = false;//关闭忙等加载
            result = result.data;
            if (!result.success){
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
          })
          //删除之后到第一页
          this.currentPage = 1;
          this.getDictionaryTypes();
        })
            .catch(result=>{
              console.log(result);
              this.$message({
                message: '系统繁忙，稍后重试！',
                type: 'error'
              });
        })

			},
			//显示编辑界面
			handleEdit: function (index, row) {
        //this.getDeptTree();//查询上级部门数据
        this.myTitle = "编辑";
				this.editFormVisible = true;
        //克隆  用于回显
				this.editForm = Object.assign({}, row);
				/*//上级部门多级选择器最后一页空数据不展示
        let dirPath = this.editForm.dirPath;
        if(dirPath){ // "/2/5/8"
          let arry = dirPath.split("/");//  ["","2","5","8"]
          let parent = [];// 定义一个空数组
          //将数据遍历去头和去尾,依次添加到数组中
          for (let i=1;i<arry.length-1;i++){
            parent.push(parseInt(arry[i]))   //[2,5]
          }
          //将数组的值重新赋值给表单中的parent属性
          this.editForm.parent = parent;
          console.log(this.editForm.parent);
        }*/
			},
			//显示新增界面
			handleAdd: function () {
        this.myTitle = "新增";
        this.editFormVisible = true;
        this.editForm = {//重置表单
          id: null,
          sn:"",
          name:""
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {//开启表单校验
					if (valid) {//校验没问题
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;//开启表单提交按钮忙等
							let para = Object.assign({}, this.editForm);//传到后端的参数
              /*//将上级部门的转换为后端需要的数据格式
              if(para.parent){
                para.parent = {"id":para.parent[para.parent.length -1]}
              }*/
							this.$http.put("/type",para)
                  .then(result=>{
                    this.editLoading = false;//关闭表单提交按钮忙等
                    result = result.data;
                    if (!result.success){
                      this.$message({
                        message: result.message,
                        type: 'error'
                      });
                    }else {
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });
                      //关闭模态框
                      this.editFormVisible = false;
                    }

                    //删除之后到第一页
                    this.currentPage = 1;
                    this.getDictionaryTypes();
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '系统繁忙，稍后重试！',
                      type: 'error'
                });
              })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				  this.$http.patch("/type",ids)
              .then(result=>{
                result = result.data;
                if (!result.success){
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
              })
          //删除之后到第一页
          this.currentPage = 1;
          this.getDictionaryTypes();
				}).catch(() => {
          console.log(result);
          this.$message({
            message: '系统繁忙，稍后重试！',
            type: 'error'
          });
				});
			}
		},
		mounted() {
			//this.getDepartments();
			this.getDictionaryTypes();
		}
	}

</script>

<style scoped>

</style>